/* Copyright (C) Yassen 2009
 *
 * text.js
 * Defines functions for managing strings.
 *
 * String  AddCommas(str)
 * String  StripTags(str)
 * String  FirstLetterToUpper(str)
 */

Lib.Text.AddCommas = function(str)
{
  var dec = str.split(".")[1]; // Decimal part of the number
  str = str.split(".")[0]; // Whole part of the number
  var negative = str.search("-") != -1;
  str = str.replace("-", "");


  var result = (negative ? "-" : "");
  var parts = [];

  while (str.length > 3)
  {
    var right = str.substr(str.length - 3, 3);
    var left = str.substr(0, str.length - 3);
    parts.push(right);
    str = left;
  }

  if (str.length != 0)
    parts.splice(0, 0, str);

  result += parts.join(",");

  if (dec != undefined)
    result += "." + dec;

  return result;
}

Number.prototype.addCommas = function() { return Lib.Text.AddCommas(this.toString()); }
String.prototype.addCommas = function() { return Lib.Text.AddCommas(this); }

Lib.Text.StripTags = function(str)
{
  return this.replace(/(<([^>]+)>)/ig,"");
}

String.prototype.stripTags = function() { return Lib.Text.StripTags(this); }

Lib.Text.FirstLetterToUpper = function(str)
{
  var result = [];
  var temp = str.split(" ");

  for (var i = 0; i < temp.length; i++)
  {
    var t = temp[i][0].toupper();
    returns.append(t + temp[i].substr(1));
  }

  return returns.join(" ");
}

String.prototype.firstLetterToUpper = function() { return Lib.Text.FirstLetterToUpper(this); }